package cn.onein.edu.util;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Random;
import java.util.UUID;

/**
 * 字符串加密，处理，取UUID等操作Util
 * 
 * @author Upton
 * 
 */
public class StringUtil {
	// MD5加密用的种子
	private static final String SEED = "sdfAD~2@5E#$@fre&*^!ffdKOUYB";

	private static char[] chars = { '0', '1', '2', '3', '4', '5', '6', '7',
			'8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k',
			'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
			'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
			'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
			'Y', 'Z'};

	/**
	 * 字符串加密成MD5，两次加密
	 * 
	 * @param source
	 *            被加密的字符串
	 * @return 加密后的MD5字符串
	 */
	public static String string2MD5(String source) {
		return secondMD5(firstMD5(source));
	}

	/**
	 * 字符串加密成MD5，一次加密，没有使用种子
	 * 
	 * @param source
	 *            被加密的字符串
	 * @return 加密后的MD5字符串
	 */
	public static String firstMD5(String source) {
		if (source == null) {
			return null;
		}

		try {
			// 第一次加密
			byte[] sourceBytes = source.getBytes("utf-8");
			MessageDigest messagedigest = MessageDigest.getInstance("MD5");
			messagedigest.update(sourceBytes);

			return bytes2HexString(messagedigest.digest());
		} catch (UnsupportedEncodingException ex) {
			throw new RuntimeException("字符串加密错误");
		} catch (NoSuchAlgorithmException ex) {
			throw new RuntimeException("字符串加密错误");
		}
	}

	/**
	 * 经过一次加密的密码进行二次加密,使用种子
	 * 
	 * @param firstMD5
	 *            客户端第一次加密
	 * @return 二次加密后的密码
	 */
	public static String secondMD5(String firstMD5) {
		if (firstMD5 == null) {
			return null;
		}

		// 第二次加密
		String newSource = SEED + firstMD5 + SEED;

		try {
			byte[] newSourceBytes = newSource.getBytes("utf-8");
			MessageDigest newMessagedigest = MessageDigest.getInstance("MD5");
			newMessagedigest.update(newSourceBytes);

			return bytes2HexString(newMessagedigest.digest());
		} catch (UnsupportedEncodingException e) {
			throw new RuntimeException("字符串加密错误");
		} catch (NoSuchAlgorithmException e) {
			throw new RuntimeException("字符串加密错误");
		}
	}

	/**
	 * 取得一个UUID字符串，没有‘-’在里面
	 * 
	 * @return 一个UUID字符串，没有‘-’在里面
	 */
	public static String getNewUUID() {
		return UUID.randomUUID().toString().replaceAll("-", "");
	}

	private static String bytes2HexString(byte[] b) {
		String ret = "";
		for (int i = 0; i < b.length; i++) {
			String hex = Integer.toHexString(b[i] & 0xFF);
			if (1 == hex.length())
				hex = "" + hex;
			ret += hex;
		}
		return ret;
	}

	public static void main(String[] args) throws Exception {
		System.out.println(StringUtil.string2MD5("test"));
	}

	public static String fenToYuan(Integer fen) {
		if (fen == null) {
			return null;
		}
		if (fen < 100) {
			if (fen < 10) {
				return "0.0" + fen.toString();
			} else {
				return "0." + fen.toString();
			}
		} else {
			StringBuffer buf = new StringBuffer(fen.toString());
			return buf.insert(fen.toString().length() - 2, ".").toString();
		}
	}

	public static String generateRandomPassword() {
		int i; // 生成的随机数
		int count = 0; // 生成的密码的长度
		int pwd_len = 6;

		StringBuffer pwd = new StringBuffer("");
		Random r = new Random();
		while (count < pwd_len) {
			// 生成随机数，取绝对值，防止生成负数，
			i = r.nextInt(chars.length); // 生成的数为0<=value<chars.length
			if (i >= 0 && i < chars.length) {
				pwd.append(chars[i]);
				count++;
			}
		}

		return pwd.toString();
	}
}
